# Maintainer: Alexey Kasatkin <alexeikasatkin@gmail.com>
# ArchLinux maintainer: Jaroslav Lichtblau <dragonlord@aur.archlinux.org>
# Contributor: dibblethewrecker dibblethewrecker.at.jiwe.dot.org
# Contributor: William Rea <sillywilly@gmail.com>
# Contributor: Petri Moisio <petri.k.moisio@gmail.com>

_realname=gdal
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.2.3
pkgrel=2
pkgdesc="A translator library for raster geospatial data formats (mingw-w64)"
arch=('any')
url="http://www.gdal.org/"
license=('custom')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-postgresql"
             "${MINGW_PACKAGE_PREFIX}-qhull")
depends=("${MINGW_PACKAGE_PREFIX}-cfitsio"
         "${MINGW_PACKAGE_PREFIX}-crypto++"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-geos"
         "${MINGW_PACKAGE_PREFIX}-giflib"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-jasper"
         "${MINGW_PACKAGE_PREFIX}-json-c"
         "${MINGW_PACKAGE_PREFIX}-libfreexl"
         "${MINGW_PACKAGE_PREFIX}-libgeotiff"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libkml"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libspatialite"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-netcdf"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-pcre"
         "${MINGW_PACKAGE_PREFIX}-poppler"
         "${MINGW_PACKAGE_PREFIX}-postgresql"
         "${MINGW_PACKAGE_PREFIX}-proj"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-xerces-c"
         "${MINGW_PACKAGE_PREFIX}-xz")
#optdepends=("${MINGW_PACKAGE_PREFIX}-postgresql")
options=('strip' 'staticlibs')

source=(http://download.osgeo.org/${_realname}/${pkgver}/${_realname}-${pkgver}.tar.gz
        0001-external-qhull-static.patch
        0002-libproj4-dll-name.patch)
sha256sums=('52f01bda8968643633016769607e6082a8ba1c746fadc2c1abe12cf7dc8f61dd'
            '6952586cd4436003748e8d99f2e8e8a660ba59bdb3c0b294493695ef57f93077'
            '1b8e2ccad84537c6ee569b254b766a25cba11fae85b891d712a91eea3c7a0f46')

prepare() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  cp -rf ${_realname}-${pkgver} build-${MINGW_CHOST}
  cd "${srcdir}/build-${MINGW_CHOST}"
  
  sed -i "s|/usr/local|${MINGW_PREFIX}|g" configure.ac
  sed -i "s|/usr|${MINGW_PREFIX}|g" configure.ac
  sed -i "s|mandir='\${prefix}/man'|mandir='\${prefix}/share/man'|g" configure.ac
  for p in m4/*.m4
  do
    sed -i "s|/usr|${MINGW_PREFIX}|g" $p
  done
  
  patch -p1 -i ${srcdir}/0001-external-qhull-static.patch
  patch -p1 -i ${srcdir}/0002-libproj4-dll-name.patch
  
  # bug: http://osgeo-org.1560.x6.nabble.com/gdal-dev-jpeg2000-jasper-error-compiling-gdal-2-1-from-git-release-branch-td5299100.html
  sed -i -e 's@uchar@unsigned char@' frmts/jpeg2000/jpeg2000_vsil_io.cpp

  sed -i 's/-lsz /-lszip -lxdr -lm/g' configure.ac
  
  ./autogen.sh
}

build() {
  cd "${srcdir}/build-${MINGW_CHOST}"

  CFLAGS+=" -fno-strict-aliasing"
  #export HDF4_INCLUDE="/usr/local/include"
  #export HDF_LIB_DIR="/usr/local/lib"
  export HDF_LIB_NAME="-L/usr/local/lib -ldf -ljpeg lsz -lz "
  export HDF5_LIB_NAME="-L/usr/local/lib -lhdf5 "

  ./configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix="/usr/local" \
    --enable-static \
    --disable-shared \
    --disable-debug \
    --without-poppler \
    --without-xerces \
    --with-webp \
    --with-spatialite=/mingw64 \
    --with-liblzma=yes \
    --with-expat-inc=/mingw64/include \
    --with-expat-lib="-L/mingw64/lib -lexpat" \
    --without-python \
    --without-perl \
    --without-threads \
    --with-jasper \
    --with-pg=/mingw64/bin/pg_config \
    --without-libkml \
    --with-png=/mingw64 \
    --with-jpeg=/mingw64 \
    --with-odbc=/c/Rtools/mingw_64/x86_64-w64-mingw32 \
    --with-freexl=/mingw64 \
    --with-static-proj4=yes \
    --with-openjpeg=/mingw64 \
    --with-kea=/usr/local/bin/kea-config \
    --with-mysql=/mingw64/bin/mariadb_config \
    --with-netcdf=/mingw64/bin/nc-config \
    --with-hdf5=/usr/local \
    --with-hdf4=/usr/local

  sed -i GDALmake.opt -e "s|EXE_DEP_LIBS.*|EXE_DEP_LIBS = \$\(GDAL_SLIB\)|g"
  sed -i GNUmakefile -e "s|\$(GDAL_ROOT)\/||g"

  read -n1 -r -p "Press any key to continue..." key

  make
#  make man
}

package () {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install
  make DESTDIR="${pkgdir}" install-man

  # install license
  install -D -m644 LICENSE.TXT "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
